package cn.xiaolang.function.data_structure;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 流量分发:
 *
 * 某一应用系统 app 分别部署在 A,B 两个机房，A 机房 100 台机器，B 机房 80 台机器，
 * 请 设计一种流量分发算法，能够根据 A，B 机房所承担的流量权重来分别分发到指定机房的机器中
 *
 * @author fuhw/Dean
 * @date 2020-04-10
 */
@Slf4j
public class FlowDistribute {


    private int cursor = 0;
    @Setter
    private List<Integer> servers;

    private int getServer() {
        if (cursor >= servers.size()) {
            cursor = 0;
        }
        int host = servers.get(cursor);
        cursor++;
        return host;
    }


    public static void main(String[] args) {
        List<Integer> servers = new ArrayList<>();
        int serverNum = 180;
        int flowNum = 10000;
        for (int i = 1; i <= serverNum; i++) {
            servers.add(i);
        }
        Collections.shuffle(servers);
        FlowDistribute flowDistribute = new FlowDistribute();
        flowDistribute.setServers(servers);
        int countA = 0;
        int countB = 0;
        for (int i = 0; i < flowNum; i++) {
            int server = flowDistribute.getServer();
            if (server <= 100) {
                countA++;
            } else {
                countB++;
            }
        }
        log.info("A机房命中流量：{},B机房命中流量：{}", countA, countB);

    }
}
